package com.txsj.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.txsj.scan.R;
import com.txsj.scan.adapter.SearchGoodsInfoAdapter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.presenter.SearchGoodsListPresenter;
import com.txsj.scan.refresh.XRefreshView;
import com.txsj.scan.view.ISearchGoodsListView;
import com.txsj.scan.widget.TitleBarView;

import java.util.List;

/**
 * Created by wangchuangjun on 2018/12/6
 * Desc: 搜索商品列表
 */

public class SearchGoodsListActivity extends BaseActivity<SearchGoodsListPresenter> implements ISearchGoodsListView,View.OnClickListener {

    RecyclerView recy_goods;
    XRefreshView xRefreshView;
    TextView tv_search;
    SearchGoodsInfoAdapter adapter;
    String searchMsg;
    EditText et_search;
    int pageNo = 1;
    boolean isRefresh,isLoadMore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_goods_list);
        intiView();
        initData();
    }

    private void initData() {

    }

    private void intiView() {
        mTitle = (TitleBarView) findViewById(R.id.tbtitle);
        tv_search = (TextView) findViewById(R.id.tv_search);
        et_search = (EditText) findViewById(R.id.et_search);
        recy_goods = (RecyclerView) findViewById(R.id.recy_goods);
        xRefreshView = (XRefreshView) findViewById(R.id.xrefreshview);


        tv_search.setOnClickListener(this);

        initEmptyView();
        //设置是否可以下拉刷新
        xRefreshView.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(true);
        // 设置是否可以自动刷新
        xRefreshView.setAutoRefresh(false);
        //开始下拉刷新
//        xRefreshView.startRefresh();
        xRefreshView.enableEmptyView(true);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                pageNo = 1;
                isRefresh = true;
                presenter.searchGoodsData(pageNo, searchMsg);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                isLoadMore = true;
                presenter.searchGoodsData(pageNo, searchMsg);
            }
        });

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recy_goods.setLayoutManager(layoutManager);
        adapter = new SearchGoodsInfoAdapter(this);
        recy_goods.setAdapter(adapter);
        adapter.setOnItemClickListener(new SearchGoodsInfoAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent = getIntent();
                intent.putExtra("data",new Gson().toJson(adapter.get(position)));
                setResult(SelectGoodsListActivity.REQUEST_SEARCH_CODE,intent);
                finish();
            }
        });

        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(SearchGoodsListActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    search(et_search.getText().toString().trim());
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_search://搜索
                search(et_search.getText().toString().trim());
                break;
        }
    }

    private void search(String search) {
        if (TextUtils.isEmpty(search)) {
            showErrorMsg("请输入要搜索的内容");
            return;
        }
        searchMsg = et_search.getText().toString().trim();
        xRefreshView.startRefresh();
    }

    @Override
    protected SearchGoodsListPresenter providePresenter() {
        return presenter = new SearchGoodsListPresenter(this);
    }

    @Override
    public Context context() {
        return this;
    }

    Button btn_empty_reload;

    /**
     * 列表数据为空时 显示的View
     */
    private void initEmptyView() {
        View emptyView = View.inflate(this, R.layout.view_empty, null);
        emptyView.setBackgroundColor(getResources().getColor(R.color.white));
        btn_empty_reload = (Button) emptyView.findViewById(R.id.btn_empty_reload);
        xRefreshView.setEmptyView(emptyView);
        btn_empty_reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xRefreshView.startRefresh();
            }
        });
    }


    @Override
    public void onLoading() {
        showLoading();
    }

    @Override
    public void onGoodsInfoListSuccess(List<GoodsInfo> data) {
        hideLoading();
        xRefreshView.stopRefresh();
        xRefreshView.stopLoadMore();
        xRefreshView.enableEmptyView(false);
        adapter.setList(data,pageNo);
        pageNo++;
    }

    @Override
    public void onLoadMoreError(String msg) {
        //加载更多失败
        hideLoading();
        xRefreshView.stopRefresh();
        xRefreshView.stopLoadMore();
        xRefreshView.enableEmptyView(false);
    }

    @Override
    public void onLoadError(String msg) {
        //商品第一次加载失败
        hideLoading();
        xRefreshView.stopRefresh();
        xRefreshView.stopLoadMore();
        xRefreshView.enableEmptyView(true);
    }

    @Override
    public void onLoadMoreEnd(String msg) {
        //商品不是第一次，加载结束
        hideLoading();
        xRefreshView.stopRefresh();
        xRefreshView.stopLoadMore();
        xRefreshView.enableEmptyView(false);
    }

    @Override
    public void onEmpty(String msg) {
        //商品第一次加载为空
        hideLoading();
        xRefreshView.stopRefresh();
        xRefreshView.stopLoadMore();
        xRefreshView.enableEmptyView(true);
    }

}
